package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.SystemNavManager;
import com.fish.anne.model.SystemNav;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class SystemNavAction extends BaseAction implements Preparable {
    private SystemNavManager systemNavManager;
    private List systemNavs;
    private SystemNav systemNav;
    private Long  navId;

    public void setSystemNavManager(SystemNavManager systemNavManager) {
        this.systemNavManager = systemNavManager;
    }

    public List getSystemNavs() {
        return systemNavs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String systemNavId = getRequest().getParameter("systemNav.navId");
            if (systemNavId != null && !systemNavId.equals("")) {
                systemNav = systemNavManager.get(new Long(systemNavId));
            }
        }
    }

    public String list() {
        systemNavs = systemNavManager.getAll();
        return SUCCESS;
    }

    public void setNavId(Long  navId) {
        this. navId =  navId;
    }

    public SystemNav getSystemNav() {
        return systemNav;
    }

    public void setSystemNav(SystemNav systemNav) {
        this.systemNav = systemNav;
    }

    public String delete() {
        systemNavManager.remove(systemNav.getNavId());
        saveMessage(getText("systemNav.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (navId != null) {
            systemNav = systemNavManager.get(navId);
        } else {
            systemNav = new SystemNav();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (systemNav.getNavId() == null);

        systemNavManager.save(systemNav);

        String key = (isNew) ? "systemNav.added" : "systemNav.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}